package com.example.p2pinvest.common;

import android.app.Activity;

import java.util.Stack;

/**
 * @author h2tc01
 * @version 1.0.0
 * 2022/8/3
 * 统一应用程序中所有的Activity的栈管理
 * 涉及到activity的添加,删除指定,删除当前,删除所有,返回栈大小的方法
 */
public class ActivityManager {

    //单例模式
    private ActivityManager() {

    }

    private static ActivityManager activityManager = new ActivityManager();

    public static ActivityManager getInstance() {
        return activityManager;
    }

    //提供栈的对象
    private Stack<Activity> activityStack = new Stack<>();

    public void add(Activity activity) {
        if (activity != null) {
            activityStack.add(activity);
        }
    }

    //删除指定的activity
    public void remove(Activity activity) {
        if (activity != null) {
            for (int i = activityStack.size() - 1; i >= 0; i--) {
                Activity currentActivity = activityStack.get(i);
                if (currentActivity.getClass().equals(activity.getClass())) {
                    currentActivity.finish(); //销毁当前的activity
                    activityStack.remove(i); //从栈空间中移除
                }
            }
        }
    }

    //删除当前的activity
    public void removeCurrent() {
        Activity activity = activityStack.lastElement();
        activity.finish();
        activityStack.remove(activity);
    }

    //删除所有的activity
    public void removeAll() {
        for (int i = activityStack.size() - 1; i >= 0; i--) {
            Activity activity = activityStack.get(i);
            activity.finish();
            activityStack.remove(activity);
        }
    }

    //返回栈的大小
    public int size() {
        return activityStack.size();
    }

}
